package com.pengcheng.taskapplication.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.mvp.view.activity.PaySuccessActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static String TAG = "MicroMsg.WXEntryActivity";
    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, ConstantValue.Wechat_Appid, true);
        Intent intent = getIntent();
        api.handleIntent(intent, this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp resp) {
        //小程序通道的回调
        if (resp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            WXLaunchMiniProgram.Resp launchMiniProResp =
                    (WXLaunchMiniProgram.Resp) resp;
            String result = launchMiniProResp.extMsg;
//20-成功，30-失败
            JSONObject jsonObject=JSON.parseObject(result);
            String status = jsonObject.getString("status");
            String message = jsonObject.getString("errMsg");
            Toast.makeText(getApplicationContext(), "支付" + ("20".equals(status) ? "成功" : " 失败"),
                    //+ ", " + message,
                    Toast.LENGTH_SHORT).show();
            if("20".equals(status)) {
                Intent intent = new Intent(this, PaySuccessActivity.class);
                startActivity(intent);
            }
        } else if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            //跳转通道的回调
            switch (resp.errCode) {
                case 0:
                    Toast.makeText(getApplicationContext(), "支付成功",
                                    //+ resp.errStr,
                            Toast.LENGTH_SHORT).show();
                    Intent intent =new Intent(this, PaySuccessActivity.class);
                    startActivity(intent);
                    break;
                case -1:
                    Toast.makeText(getApplicationContext(), "支付取消",
                                    //+ resp.errStr,
                            Toast.LENGTH_SHORT).show();
                    break;
                case -2:
                    Toast.makeText(getApplicationContext(), "请求失败,",
                            //+ resp.errStr,
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
        finish();
    }
}
